<template>
    <div style="width: 100%; text-align: center;">
        <p>CI/CD by Changan 为默认插件，通过git上传代码之后，自动编译。请在镜像管理中发布该应用。</p>
        该插件无需配置, 请直接保存
        <div style="margin-top: 8px">
            <p>非titan工程，请在此处下载对应gitlab-ci.yml模板<Button :loading="gitLoading" @click="downlaod">download</Button></p>
        </div>
    </div>
</template>

<script>
    export default {
        data(){
            return {
                gitLoading: false
            }
        },
        props:['tempApp'],
        created(){

        },
        methods:{
            initData(data){
            },
            downlaod(){
                this.gitLoading = true;
                let _this = this;
                let fileName = this.tempApp.appInfo.appName + ".zip";
                let xhr = new XMLHttpRequest();
                xhr.open('GET', '/mdo/api/v1/app/download/' + this.tempApp.appInfo.id + '/ci', true);
                xhr.responseType = 'blob';
                this.$nextTick(()=>{
                    xhr.onload = function () {
                        if (this.getResponseHeader("content-disposition") && this.getResponseHeader("content-disposition").indexOf('filename') > -1) {
                            fileName = this.getResponseHeader("content-disposition").split(";")[1].split("filename=")[1];
                            fileName = fileName.replace(new RegExp('"', 'g'),'').replace(new RegExp('\'', 'g'),'');
                        }
                        if (this.status === 200) {
                            let blob = this.response;
                            if (navigator.appVersion.toString().indexOf('.NET') > 0) {
                                window.navigator.msSaveBlob(blob, fileName);
                            } else {
                                let body = document.body;
                                let a = document.createElement('a');
                                a.download = fileName;
                                a.href = URL.createObjectURL(blob);
                                body.appendChild(a);
                                a.click();
                                body.removeChild(a);
                            }
                        } else {
                            _this.$Message.error('应用下载失败');
                        }
                        _this.gitLoading = false;
                    };
                });
                xhr.send();
            },
            getData(){
                return new Promise((resolve)=>{
                    resolve({
                        code: 'changan',
                        groupName: 'CI/CD by Changan',
                        config: null
                    });
                })
            }
        }
    }
</script>

<style lang="less">
</style>